package dtaskboard.server.util;

import dtaskboard.client.dto.RoleClass;
import dtaskboard.entities.Role;
import dtaskboard.entities.User;
import dtaskboard.server.SessionManager;

public class SecurityUtil {
  public static boolean hasAdmin(String sessionId) {
    User user = SessionManager.get().userId(sessionId);
    boolean hasRole = false;
    for (Role r : user.getRoles()) {
      if (r.getRoleClass() == RoleClass.ADMIN) {
        hasRole = true;
        break;
      }
    }
    return hasRole;
  }

  public static boolean hasRole(String sessionId, RoleClass role,
      String projectName) {
    User user = SessionManager.get().userId(sessionId);
    for (Role r : user.getRoles()) {
      if (r.getRoleClass().equals(role) && r.getProject() != null
          && projectName.equals(r.getProject().getName())) {
        return true;
      }
    }
    return false;
  }

}
